package com.itheima.reggie.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //注意：由于静态资源目前不是在webapp目录下，所以放行静态资源的时候一定要有classpath:

        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");

        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

   /*
   扩展mvc框架的消息转换器
    */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { //converters springmvc 默认有的消息类型转换器，
        //1. 创建消息类型转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        //2. 创建对象类型转换器，并且设置给消息类型转换器
        converter.setObjectMapper(new JacksonObjectMapper());

        //3. 把消息类型转换器放在converters最前面
        converters.add(0,converter);
    }
}
